﻿using System;
using System.Collections.Generic;
using System.Text;
using EShop.Model;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace EShop.Client
{
    class Serial
    {
        public static EshopConfigInfo ReadConfig()
        {
            EshopConfigInfo oConfigInfo = null;
            string strConfigPath = System.Windows.Forms.Application.StartupPath + "\\EshopConfig.bin";
            //判断文件是否存在
            if (!File.Exists(strConfigPath))
                return null;

            //FileStream fs = new FileStream(strConfigPath, FileMode.OpenOrCreate);
            //StreamReader sr = new StreamReader(fs);
            //string s = sr.ReadToEnd();
            //s = Common.SystemLibrary.SystemEncode.GetString(Safety.RSA.RSADecrypt(Common.SystemLibrary.SystemEncode.GetBytes(s)));//解密
            //sr.Close();
            //StreamWriter sw = new StreamWriter(fs);
            //sw.Write(s);
            //sw.Close();
            //fs.Close();

            System.Windows.Forms.MessageBox.Show(strConfigPath);
            System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();
            //打开一个文件流
            using (Stream stream = new FileStream(strConfigPath, FileMode.Open, FileAccess.Read))
            {
                //反序列化
                oConfigInfo = (EshopConfigInfo)formatter.Deserialize(stream);
            }
            return oConfigInfo;
        }

        public static void SaveConfig(EshopConfigInfo oConfigInfo)
        {
            System.Runtime.Serialization.IFormatter formatter = new BinaryFormatter();

            //创建一个文件流
            string strConfigPath = System.Windows.Forms.Application.StartupPath + "\\EshopConfig.bin";
            using (Stream stream = new FileStream(strConfigPath, FileMode.Create, FileAccess.Write))
            {
                Encoding enc = Common.SystemLibrary.SystemEncode;

                //序列化
                formatter.Serialize(stream, oConfigInfo);
            }
            //FileStream fs = new FileStream(strConfigPath, FileMode.OpenOrCreate);
            //StreamReader sr = new StreamReader(fs);
            //string s = sr.ReadToEnd();
            //s = Common.SystemLibrary.SystemEncode.GetString(Safety.RSA.RSAEncrypt(Common.SystemLibrary.SystemEncode.GetBytes(s)));//加密
            //sr.Close();
            //StreamWriter sw = new StreamWriter(fs);
            //sw.Write(s);
            //sw.Close();
            //fs.Close();

        }
    }
}
